package com.gin.no_gay_alliance.adpter;

import android.content.Context;
import android.content.DialogInterface;
import android.view.View;
import android.widget.PopupMenu;

import androidx.annotation.NonNull;

import com.gin.common.utils.TimeUtils;
import com.gin.jackson.utils.ObjectUtils;
import com.gin.no_gay_alliance.R;
import com.gin.no_gay_alliance.activity.TopicReadActivity;
import com.gin.no_gay_alliance.activity.UserCenterActivity;
import com.gin.no_gay_alliance.adpter.base.BaseBindingRecyclerViewAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.database.entity.ReportingRecord;
import com.gin.no_gay_alliance.databinding.ItemReportingRecordBinding;
import com.gin.no_gay_alliance.utils.AlertDialogUtils;
import com.gin.no_gay_alliance.utils.ViewUtils;

import java.util.List;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * 举报记录适配器
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2024/1/24 11:14
 **/
public class ReportingRecordAdapter extends BaseBindingRecyclerViewAdapter<ItemReportingRecordBinding, ReportingRecord> implements View.OnClickListener {
    public ReportingRecordAdapter(Context context) {
        super(context);
    }

    public ReportingRecordAdapter(@NonNull Context context, @NonNull List<ReportingRecord> data) {
        super(context, data);
    }

    /**
     * 绑定数据
     *
     * @param holder
     * @param b        绑定对象
     * @param position 位置
     * @param item
     */
    @Override
    protected void onBindViewHolder(ViewHolder<ItemReportingRecordBinding> holder,
                                    @NonNull ItemReportingRecordBinding b, int position, ReportingRecord item) {
        b.tvContent.setText(String.format(Locale.CHINA, "回复内容: %s", item.content));
        b.tvReason.setText(String.format(Locale.CHINA, "理由: %s", item.reason));
        b.tvReportedUser.setText(item.username);
        b.tvTitle.setText(item.title);
        b.tvTimestamp.setText(TimeUtils.format(item.timestamp, TimeUnit.SECONDS, TimeUtils.SHORT_FORMATTER));

        ViewUtils.setGone(ObjectUtils.isEmpty(item.reason), b.tvReason);
        ViewUtils.setGone(ObjectUtils.isEmpty(item.username), b.tvReportedUser);
        ViewUtils.setGone(ObjectUtils.isEmpty(item.title), b.tvTitle);

        b.getRoot().setTag(item);
        b.getRoot().setOnClickListener(this);

    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    @Override
    public void onClick(View v) {
        // 点击项目后弹窗： 打开被举报人用户中心，打开原回复，删除本条记录
        ReportingRecord reportingRecord = (ReportingRecord) v.getTag();
        // 下拉框
        PopupMenu popupMenu = new PopupMenu(context, v);
        popupMenu.inflate(R.menu.reporting_record_popup_menu);

        // 如果没有用户id ，隐藏用户中心选项
        popupMenu.getMenu().findItem(R.id.menu_item_user_center).setVisible(reportingRecord.userId != null);

        popupMenu.setOnMenuItemClickListener(item -> {
            // 用户中心
            if (item.getItemId() == R.id.menu_item_user_center) {
                UserCenterActivity.startActivity(context, reportingRecord.userId);
                return true;
            }
            //  回复
            if (item.getItemId() == R.id.menu_item_reply) {
                if (reportingRecord.replyId > 0) {
                    // 有回复id，以单回复模式打开
                    TopicReadActivity.startActivity(context, reportingRecord.replyId);
                } else {
                    // 没有回复id，打开主题
                    TopicReadActivity.startActivity(context, reportingRecord.title, reportingRecord.topicId);
                }
                return true;
            }
            // 删除本条
            if (item.getItemId() == R.id.menu_item_delete) {

                AlertDialogUtils.showConfirm(context, "删除举报记录", "确认删除?", (dialog, which) -> {
                    if (which == DialogInterface.BUTTON_POSITIVE) {
                        MyApplication.app.ngaDatabase.reportingRecordDao().delete(reportingRecord);
                        int position = data.indexOf(reportingRecord);
                        handler.post(() -> {
                            data.remove(position);
                            notifyItemRemoved(position);
                        });
                    }
                });
                return true;
            }
            return false;
        });

        popupMenu.show();
    }
}
